import { plugin } from "alemon";
import data from '../../model/fantasyData.js';
import {
  __PATH,
  Add_HP,
  Read_player,
  existplayer,
  Read_equipment,
  Write_equipment,
  Write_player,
  Read_bag,
  Write_bag,
  Read_mapevent,
  Read_mapmove,
  Set_battleobject,
  Set_battleplayer,
  Judge_up,
  Add_bag,
  Read_state,
  Write_state,
  Read_battleobject,
  Read_battleplayer,
  Write_battleobject,
  Write_battleplayer,
  Read_thingtype,
  ifmingzhong,
  ifbaoji,
  baoji,
  battle_round,
  battle_add,
  delete_battleplayer,
  delete_battleobject,
  Read_skill,
  Read_proplist,
  Read_monster,
  Write_team,
  Read_team,
  object_time,
  sleep,
  Read_teamIndex,

} from '../../model/fantasy.js';
import { basename } from "path/win32";
import { userInfo } from "node:os";
import { REPL_MODE_SLOPPY } from "node:repl";


export class Userteam extends plugin {
  constructor() {
    super({
      priority: 600,
      rule: [
                {
                    reg: /^创建队伍.*$/,
                    fnc: 'create_team'
                },
                {
                    reg: /^解散队伍$/,
                    fnc: 'dismiss_team'
                },
                {
                    reg: /^加入队伍.*$/,
                    fnc: 'join_team'
                },
                {
                    reg: /^退出队伍.*$/,
                    fnc: 'leave_team'
                },
                {
                    reg: /^队伍信息$/,
                    fnc: 'team_information'
                },
                {
                    reg: /^进入战斗$/,
                    fnc: 'start_battle'
                },
                {
                    reg: /^\/(普攻|技能1|技能2|技能3|技能4)$/,
                    fnc: 'battle_attack'
                },
                
      ],
    });
    
  }

    //创建队伍
    async create_team(e){
    e.cmd_msg = e.cmd_msg.replace("/", "");
    let usr_qq = e.user_id;
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) {
      return;
    }
    if(await Read_state(usr_qq) == "组队战斗"){
      return;
    }
    let player = await Read_player(usr_qq);
    const id = player["id"];
    const team_name = e.cmd_msg.replace("创建队伍", "");
    let team = await Read_team(usr_qq);
    const len = team.length;
    for(let i = 0; i < len; i++){
      if(team[i]["队长"]["名称"] == id){
        e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n你已经是队长！`);
        return;
      }else if(team[i]["队员"].findIndex(obj => obj["名称"] == id) >= 0){
        e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n你已经在队伍中！`);
        return;
      }else if(team[i]["队伍名"] == team_name){
        e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n队伍名称重复！`);
      }
    }
    team.push({"队伍名": team_name, "队长": {"名称": id,"间隔": "", "时间": ""}, "队员": []});
    e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n你创建了队伍[${team_name}]\nTips:发送[加入队伍${team_name}]加入该队伍`);
    await Write_team(usr_qq, team);
    return;
    }


    //解散队伍
    async dismiss_team(e){
    e.cmd_msg = e.cmd_msg.replace("/", "");
    let usr_qq = e.user_id;
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) {
      return;
    }
    if(await Read_state(usr_qq) == "组队战斗"){
      return;
    }
    let player = await Read_player(usr_qq);
    const id = player["id"];
    let team = await Read_team(usr_qq);
    const len = team.length;
    for(let i = 0; i < len; i++){
      if(team[i]["队长"]["名称"] == id){
        let team_name = team[i]["队伍名"];
        team.splice(i, 1);
        e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n你解散了队伍[${team_name}]`);
        await Write_team(usr_qq, team);
        return;
      }else if(team[i]["队员"].findIndex(obj => obj["名称"] == id) >= 0){
        e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n你不是队长！`);
        return;
      }
    }
    e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n你不在队伍中！`);
    return;
    }


    //加入队伍
    async join_team(e){
    e.cmd_msg = e.cmd_msg.replace("/", "");
    let usr_qq = e.user_id;
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) {
      return;
    }
    if(await Read_state(usr_qq) == "组队战斗"){
      return;
    }
    let player = await Read_player(usr_qq);
    const id = player["id"];
    const team_name = e.cmd_msg.replace("加入队伍", "");
    let team = await Read_team(usr_qq);
    const len = team.length;
    for(let i = 0; i < len; i++){
      if(team[i]["队长"]["名称"] == id){
        team.splice(i, 1);
        e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n你已是队伍队长！`);
        return;
      }else if(team[i]["队员"].findIndex(obj => obj["名称"] == id) >= 0){
        e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n你已经在队伍中！`);
        return;
      }else if(team[i]["队员"].length == 3){
        e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n该队伍已满！`);
        return;
      }
    }
    const index = team.findIndex(obj => obj["队伍名"] == team_name);
    if(index == -1){
      e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n不存在队伍名[${team_name}]！`);
    }else{
      team[index]["队员"].push({"名称": id,"间隔": "", "时间": ""});
      e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n你加入了队伍[${team_name}]Tips:发送[退出队伍]退出该队伍`);
    }
    await Write_team(usr_qq, team);
    return;
    }


    //离开队伍
    async leave_team(e){
    e.cmd_msg = e.cmd_msg.replace("/", "");
    let usr_qq = e.user_id;
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) {
      return;
    }
    if(await Read_state(usr_qq) == "组队战斗"){
      return;
    }
    let player = await Read_player(usr_qq);
    const id = player["id"];
    let team = await Read_team(usr_qq);
    const len = team.length;
    for(let i = 0; i < len; i++){
      if(team[i]["队长"] == id){
        team.splice(i, 1);
        e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n你是队长，只能解散队伍！`);
        return;
      }else if(team[i]["队员"].findIndex(obj => obj["名称"] == id) >= 0){
        if (index !== -1) {  
        team[i]["队员"].splice(index, 1); 
        await Write_team(usr_qq, team);
        }
        e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n你退出了队伍[${team[i]["队伍名"]}]`);
        return;
      }
    }
    e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n你不在队伍中！`);
    return;
    }

    async team_information(e){
    e.cmd_msg = e.cmd_msg.replace("/", "");
    let usr_qq = e.user_id;
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) {
      return;
    }
    let player = await Read_player(usr_qq);
    const id = player["id"];
    let team = await Read_team(usr_qq);
    const len = team.length;
    for(let i = 0; i < len; i++){
      if(team[i]["队长"]["名称"] == id || team[i]["队员"].findIndex(obj => obj["名称"] == id) >= 0){
        let word1 = `======队伍信息[${team[i]["队伍名"]}]======\n————————————————`;
        let id0 = team[i]["队长"]["名称"];
        let name0 = await Read_player(id0);
        let name = name0["昵称"];
        let word2 = `\n队长: [${name}]\nid: ${id0}`;
        for(let x = 0; x < team[i]["队员"].length; x++){
          id0 = team[i]["队员"][x]["名称"];
          name0 = await Read_player(id0);
          name = name0["昵称"];
          word2 += `\n队员${x + 1}: [${name}]\nid: ${id0}`
        }
        e.reply(word1 + word2);
        return;
      }
    }
    e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n你不在队伍中！`);
    return;
    }



    async start_battle(e){
    e.cmd_msg = e.cmd_msg.replace("/", "");
    let usr_qq = e.user_id;
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) {
      return;
    }
    if(await Read_state(usr_qq) == "组队战斗"){
      return;
    }
    let player = await Read_player(usr_qq);
    const id = player["id"];
    let team = await Read_team(usr_qq);
    const len = team.length;
    for(let i = 0; i < len; i++){
      if(team[i]["队长"]["名称"] == id){
        await Set_battleobject(team[i]["队长"]["名称"], "莉莉丝梦魇", "20-21");
        let object = await Read_battleobject(team[i]["队长"]["名称"]);
        let speed = object["速度"];
        team[i]["对象"] = {"间隔": 10,"时间": "","被击": 0};
        e.reply(`【队伍[${team[i]["队伍名"]}]】\n————————————\n已确认进入战斗，正在加载怪物信息`);
        await Set_battleplayer(team[i]["队长"]["名称"]);
        let player0 = await Read_battleplayer(team[i]["队长"]["名称"]);
        team[i]["队长"]["间隔"] = 10 * (player0["速度"] + speed)/ (2 * player0["速度"]);
        await Write_state(team[i]["队长"]["名称"], "组队战斗");
        for(let x = 0; x < team[i]["队员"].length; x++){
        await Set_battleplayer(team[i]["队员"][x]["名称"]);
        player0 = await Read_battleplayer(team[i]["队员"][x]["名称"]);
        team[i]["队员"][x]["间隔"] = 10 * (player0["速度"] + speed)/ (2 * player0["速度"]);
        await Write_state(team[i]["队员"][x]["名称"], "组队战斗");
        }
        await Write_team(usr_qq, team);
        await sleep(1000);
        e.reply(`【队伍[${team[i]["队伍名"]}]】\n————————————\n加载完毕，战斗开始！\nTips:发送[#普攻]普通攻击\n发送[#技能1|2|3|4]释放技能`);
        await sleep(3000);
        object_time(usr_qq, e);
        return;
      }else if(team[i]["队员"].includes(id)){
        e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n你不是队长！`);
        return;
      }
    }
    e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n你不在队伍中！`);
    return;
    }


    async battle_attack(e){
    let usr_qq = e.user_id;
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) {
      return;
    }
    if(await Read_state(usr_qq) !== "组队战斗"){
      return;
    }
    let player = await Read_battleplayer(usr_qq);
    if(player["当前生命"] == 0){
      e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n你已经奄奄一息！`);
      return;
    }
    let player0 = await Read_player(usr_qq);
    let team0 = await Read_team(usr_qq);
    let teamindex = await Read_teamIndex(usr_qq);
    let object = await Read_battleobject(team0[teamindex]["队长"]["名称"]);
    let object1 = await battle_add(object);
    let player1 = await battle_add(player);
    if(e.cmd_msg == "普攻"){
      player1["当前技能"] = "普攻";
    }else if(e.cmd_msg.substring(0, 2) == "技能"){
      if(e.cmd_msg.slice(2) == "1" || e.cmd_msg.slice(2) == "2" || e.cmd_msg.slice(2) == "3" || e.cmd_msg.slice(2) == "4"){
        player1["当前技能"] = player0["技能"].find((item) => item.快捷 === e.cmd_msg.slice(2)).技能名
        if(player1["当前技能"] == null || player1["当前技能"] == undefined){
        e.reply(`【${player0.昵称} Lv.${player0.等级}】\n————————————\n你尚未学习该技能哦~\nTips:发送[我的技能]查看技能详情`)
        }
      }else{
        e.reply(`【${player0.昵称} Lv.${player0.等级}】\n————————————\n你的技能指令格式不正确\nTips:发送[我的技能]查看技能详情`)
        return;
      }
    }
    //出手时间判断
    if(team0[teamindex]["队长"]["名称"] == usr_qq){
      if(team0[teamindex]["队长"]["时间"] > 0){
        e.reply(`【${player0.昵称} Lv.${player0.等级}】\n————————————\n离下一次出手 ${team0[teamindex]["队长"]["时间"]} 秒`);
        return;
      }
    }else{
      const item0 = team0[teamindex]["队员"].find(obj => obj["名称"] == usr_qq);
      if(item0["时间"] > 0){
        e.reply(`【${player0.昵称} Lv.${player0.等级}】\n————————————\n离下一次出手 ${item0["时间"]} 秒`);
        return;
      }
    }


    //技能冷却判断
    if(player1["当前技能"] !== "普攻"){
      const item = player1["技能"].find(item => item["技能名"] == player1["当前技能"]);
    if(item["时间"] > 0){
      e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n技能[${player1["当前技能"]}]冷却中\n剩余时间 ${item["时间"] * 10} 秒`);
      return;
    }
    }
    //如果对面为一个
    let word = await battle_round(player1, object1);
    player["当前生命"] = player1["当前生命"];
    player["当前魔力"] = player1["当前魔力"];
    player["增益"] = player1["增益"];
    await Write_battleplayer(usr_qq, player);
    object["当前生命"] = object1["当前生命"];
    object["当前魔力"] = object1["当前魔力"];
    object["增益"] = object1["增益"];
    object["技能"] = object1["技能"];
    await Write_battleobject(team0[teamindex]["队长"]["名称"], object);
    if(team0[teamindex]["队长"]["名称"] = usr_qq){
      team0[teamindex]["队长"]["时间"] = team0[teamindex]["队长"]["间隔"];
    }else{
      let index = team0[teamindex]["队员"].findIndex(obj => obj["名称"] == usr_qq);
      team0[teamindex]["队员"][index]["时间"] = team0[teamindex]["队员"][index]["间隔"];
    }
    await Write_team(usr_qq, team0);
    e.reply(word);
    }



















}